import Vue from 'vue';
var rateStage = require("./rateStage.js");
var mpheader = require("../components/mpheader.js");
var num = require("../components/number.js");

var appSelectVue;
var vueRateArray = [];
var checkedRateObject;
// 實例化申請金額 期數選擇界面
function createSelectVue(rateArray1, commonData, Vue2Filters) {
    vueRateArray = rateArray1;
    var stagediv = rateStage.getStagediv(vueRateArray);
    var mheder1 = mpheader.getMPheader({
        "titleText": "申请信用贷"
    }, function() {
        commonData.step = 1;
    });
    //    Vue.use(Vue2Filters);
    appSelectVue = new Vue({
        el: "#app_select",
        data: {
            rateArray: vueRateArray,
            loanAmoun: "",
            commonData: commonData,
            agreeCheck: true,
            limit: "请输入600-20000",
            amtLowLimit: 600,
            amtUpLimit: 20000,
            isTishi: false,
            checkedRateObject: ""

        },
        computed: {

        },
        beforeMount: function() {
            console.log("amountslect beforeMount 渲染前");
        },
        mounted: function() {
            console.log("amountslect mounted 加载完");

        },
        components: {
            "stagediv": stagediv,
            "mpheader": mheder1
        },
        methods: {
            loanAmounChange: function() {
                console.log(this.loanAmoun);
                if (parseFloat(this.loanAmoun) < parseFloat(this.amtLowLimit) || parseFloat(this.loanAmoun) > parseFloat(this.amtUplimit)) {
                    this.isTishi = true;
                } else {
                    this.isTishi = false;
                }
                $.publish("loanAmounChange", [checkedRateObject, this.loanAmoun]);
            },
            loanAmounInput: function() {
                if (parseFloat(this.loanAmoun) > parseFloat(this.amtLowLimit) && parseFloat(this.loanAmoun) <= parseFloat(this.amtUpLimit)) {
                    this.isTishi = false;
                }
                if (parseFloat(this.loanAmoun) > parseFloat(this.amtUpLimit)) {
                    this.loanAmoun = this.amtUpLimit;
                } else if (this.loanAmoun.search(/\.\d{2}/) != -1) {
                    this.loanAmoun = this.loanAmoun.match(/\d{1,}\.\d{2}/)[0];
                }

                openFillInfo();
                setEcahMoney();
            },
            agreeCheckChange: function() {
                var that = this;
                setTimeout(function() {
                    openFillInfo(that);
                }, 200);

            },
            rateChange: function(stageNum) {

                console.log(stageNum);
                for (var i = 0, l = this.rateArray.length; i < l; i++) {
                    if (this.rateArray[i].stage == stageNum) {
                        this.rateArray[i].check = "checked";
                        checkedRateObject = this.rateArray[i];
                        window.prdtNo = checkedRateObject.prdtNo;
                    } else {
                        this.rateArray[i].check = "";
                    }
                }
                $.publish("loanAmounChange", {
                    "aaa": "111111"
                });
            },
            checkPlan: function() {
                console.log("checkPlan");
                commonData.step = 3;
            },
            toFillInfo: function(event) {
                if ($(event.target).hasClass("weui-btn_disabled")) {
                    return false;
                } else {
                    commonData.step = 4;
                }

            },
            showContract: function() {
                commonData.step = 6;
            }

        }
    });
    subscribeRateChange();
    return appSelectVue;
}


// 填好金额点亮下一步
function openFillInfo() {

    if (parseFloat(appSelectVue.loanAmoun) >= appSelectVue.amtLowLimit && parseFloat(appSelectVue.loanAmoun) <= parseFloat(appSelectVue.amtUpLimit) && appSelectVue.agreeCheck) {
        $("#toFillInfo").removeClass("weui-btn_disabled");
    } else {
        $("#toFillInfo").addClass("weui-btn_disabled");
    }
}


// 设费率数组
function setRateArray(yrateArray, defaultStage) {
    var dstage = defaultStage ? defaultStage : 6;

    for (var i = 0, l = yrateArray.length; i < l; i++) {
        var theRateObject = {
            classVal: "each_stage",
            check: "",
            repayment: "0.00"
        };

        theRateObject = setEachRate(theRateObject, yrateArray[i]);
        if (yrateArray[i].stageCount == dstage) {
            theRateObject.check = "checked";
            checkedRateObject = theRateObject;
            appSelectVue.checkedRateObject = checkedRateObject;
            window.prdtNo = checkedRateObject.prdtNo;
        }

        vueRateArray.push(theRateObject);
    }

    appSelectVue.rateArray = vueRateArray;
}

// 设置每期的费率和折扣
function setEachRate(theRateObject, rateArrayObject) {
    var eachcustRate = ((rateArrayObject.custRate / rateArrayObject.stageCount) + 0.0001).toFixed(2);
    theRateObject.custRate = rateArrayObject.custRate;
    theRateObject.stage = rateArrayObject.stageCount;
    theRateObject.saleRate = eachcustRate;
    theRateObject.prdtNo = rateArrayObject.prdtNo;
    if (rateArrayObject.stageCount == 6 && parseFloat(eachcustRate) < 0.9) {
        // 设置折扣
        theRateObject.primeRate = "0.90";
        theRateObject.stageSale = (parseFloat(eachcustRate) * 10 / 0.9).toFixed(2);
        theRateObject.rebateStyle = "rebate";
    } else if (rateArrayObject.stageCount == 12 && parseFloat(eachcustRate) < 0.75) {
        // 设置折扣
        theRateObject.primeRate = "0.75";
        theRateObject.stageSale = (parseFloat(eachcustRate) * 10 / 0.75).toFixed(2);
        theRateObject.rebateStyle = "rebate";
    } else {
        theRateObject.primeRate = "";
        theRateObject.stageSale = "";
        theRateObject.rebateStyle = "";
    }

    return theRateObject;
}

// 计算每期还款额
function setEcahMoney() {
    var loanAmoun = appSelectVue.loanAmoun == "" ? 0.00 : appSelectVue.loanAmoun;
    for (var i = 0, l = vueRateArray.length; i < l; i++) {
        var contentMonet = parseFloat(loanAmoun * (parseFloat(vueRateArray[i].custRate) / 100) + 0.00001 + parseFloat(loanAmoun)).toFixed(2);
        var eachMoney = num.hold2bit(parseFloat(contentMonet) / parseFloat(vueRateArray[i].stage));
        vueRateArray[i].repayment = eachMoney;
        vueRateArray[i].contentMonet = contentMonet;
    }

}

// 设置上下限
function setLimit(rateArray) {
    var amtLowLimit = parseFloat(rateArray[0].amtLowLimit);
    var amtUpLimit = parseFloat(rateArray[0].amtUplimit);
    var custRate = parseFloat(rateArray[0].custRate);
    for (var i = 1, l = rateArray.length; i < l; i++) {
        if (parseFloat(rateArray[i].amtLowLimit) < amtLowLimit) {
            amtLowLimit = parseFloat(rateArray[i].amtLowLimit);

        }
        if (parseFloat(rateArray[i].amtUplimit) > amtUpLimit) {
            amtUpLimit = parseFloat(rateArray[i].amtUplimit);
            custRate = parseFloat(rateArray[i].custRate);
        }
    }


    amtUpLimit = num.hold2bit(Math.floor(amtUpLimit / (1 + custRate / 100)));
    amtLowLimit = num.hold2bit(amtLowLimit);
    appSelectVue.limit = "请输入" + amtLowLimit + "-" + amtUpLimit;
    appSelectVue.amtLowLimit = amtLowLimit;
    appSelectVue.amtUpLimit = amtUpLimit;
}

//  监听期数改变 触发还款金额改变
function subscribeRateChange(argument) {
    $.subscribe("rateChange", function(event, newcheckedRateObject) {
        checkedRateObject = newcheckedRateObject;
        appSelectVue.checkedRateObject = checkedRateObject;
        window.prdtNo = checkedRateObject.prdtNo;
        $.publish("loanAmounChange", [checkedRateObject, appSelectVue.loanAmoun]);
    });
}

module.exports = {
    createSelectVue: createSelectVue,
    setLimit: setLimit,
    setRateArray: setRateArray
};